//
//  TripDetialCell.m
//  XhlTrip
//
//  Created by dllo on 15/8/28.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TripDetialCell.h"
#import "UIButton+WebCache.h"

@implementation TripDetialCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.tipDic = [NSMutableDictionary dictionary];
        
        self.desLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.desLabel];
        [_desLabel release];
        
        self.scroller = [[UIScrollView alloc] init];
        [self.contentView addSubview:self.scroller];
        [_scroller release];
        
        
        self.desLabel.numberOfLines = 0;
        self.desLabel.font = [UIFont fontWithName:@"Verdana-Italic" size:16 * Width];
        
        
    }
    return self;
}

- (void)createbutton:(NSMutableDictionary *)dictionary
{
    NSMutableArray *photoArr = [dictionary objectForKey:@"notes"];
    
    /* 给scroller赋值 上面创建图片 */
    CGFloat pWidth = 0;
    
    /* 要不得重叠 */
    if (self.scroller.subviews != nil) {
        for (UIButton *button in self.scroller.subviews) {
            [button removeFromSuperview];
        }
    }
    for (int i = 0; i < [photoArr count]; i++) {
        
        if (![[photoArr[i] objectForKey:@"photo_url"] isKindOfClass:[NSNull class]]) {
            CGFloat photoWith = [[self class] widthWithImage:photoArr[i] height:100];
            UIButton *photoButton = [UIButton buttonWithType:UIButtonTypeSystem];
            [photoButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            
            NSString *urlStr =[photoArr[i] objectForKey:@"photo_url"];
            photoButton.frame = CGRectMake( 3 * i * Width + pWidth, 0, photoWith, 100 * Height);
            [photoButton sd_setBackgroundImageWithURL:[NSURL URLWithString:urlStr] forState:UIControlStateNormal];
            [self.scroller addSubview:photoButton];
            pWidth = pWidth + photoWith;
            photoButton.tag = i + 1;
        }
       
    }
    
    self.scroller.contentSize = CGSizeMake(pWidth, 0);
    [self.scroller resignFirstResponder];
}

- (void)buttonAction:(UIButton *)sender
{
    NSArray *array = [NSArray arrayWithArray:[_tipDic objectForKey:@"notes"]];
    self.block(array, sender.tag);
}

- (void)setTipDic:(NSMutableDictionary *)tipDic
{
    if (_tipDic != tipDic) {
        _tipDic = tipDic;
    }
    if ([_tipDic objectForKey:@"description"] != nil) {
        self.desLabel.text = [_tipDic objectForKey:@"description"];
    }
  
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat heightText;
    

    heightText = [[self class] heightWithText:self.desLabel.text  width:self.contentView.frame.size.width - 20];
    self.desLabel.frame = CGRectMake(10 * Width, 10 * Height, self.contentView.frame.size.width - 20 * Width, heightText);
    self.scroller.frame = CGRectMake(10 * Width, 10 * Height + heightText + 10 * Height, self.frame.size.width - 20 * Width, 100 * Height);
    
    
}


+ (CGFloat)heightWithText:(NSString *)text width:(CGFloat )width
{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Verdana-Italic" size:16 * Width]} context:nil];
    return rect.size.height;
}

#pragma mark - 图片高度自适应
+ (CGFloat)widthWithImage:(NSDictionary *)magedic height:(CGFloat)height
{
    /* 等比例缩放算出高度 */
    CGFloat newh = [[magedic objectForKey:@"width"] floatValue] / [[magedic objectForKey:@"height"] floatValue] * height;
    /* 返回高度 */
    return newh;
    
}



- (void)dealloc
{
    Block_release(_block);
    [_tipDic release];
    [_desLabel release];
    [_scroller release];
    [super dealloc];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
